{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "bed2046d",
   "metadata": {},
   "source": [
    "# New Scale Transformations: `log2` and `symlog`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "b545b7bd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"hn4yaM\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"library\">\n",
       "       if(!window.letsPlotCallQueue) {\n",
       "           window.letsPlotCallQueue = [];\n",
       "       }; \n",
       "       window.letsPlotCall = function(f) {\n",
       "           window.letsPlotCallQueue.push(f);\n",
       "       };\n",
       "       (function() {\n",
       "           var script = document.createElement(\"script\");\n",
       "           script.type = \"text/javascript\";\n",
       "           script.src = \"https://cdn.jsdelivr.net/gh/JetBrains/lets-plot@v4.1.0/js-package/distr/lets-plot.min.js\";\n",
       "           script.onload = function() {\n",
       "               window.letsPlotCall = function(f) {f();};\n",
       "               window.letsPlotCallQueue.forEach(function(f) {f();});\n",
       "               window.letsPlotCallQueue = [];\n",
       "               \n",
       "               \n",
       "           };\n",
       "           script.onerror = function(event) {\n",
       "               window.letsPlotCall = function(f) {};\n",
       "               window.letsPlotCallQueue = [];\n",
       "               var div = document.createElement(\"div\");\n",
       "               div.style.color = 'darkred';\n",
       "               div.textContent = 'Error loading Lets-Plot JS';\n",
       "               document.getElementById(\"hn4yaM\").appendChild(div);\n",
       "           };\n",
       "           var e = document.getElementById(\"hn4yaM\");\n",
       "           e.appendChild(script);\n",
       "       })();\n",
       "   </script>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%useLatestDescriptors\n",
    "%use lets-plot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "23da856d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Lets-Plot Kotlin API v.4.5.0. Frontend: Notebook with dynamically loaded JS. Lets-Plot JS v.4.1.0."
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "LetsPlot.getInfo()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "d3c457ce",
   "metadata": {},
   "outputs": [],
   "source": [
    "fun getData(n: Int): Map<String, List<Double>> {\n",
    "    val x = (-n..n).map(Int::toDouble)\n",
    "    val y = x.map {\n",
    "        when {\n",
    "            it == 0.0 -> it\n",
    "            it > 0 -> exp(it)\n",
    "            else -> -exp(-it)\n",
    "        }\n",
    "    }\n",
    "    return mapOf(\"x\" to x, \"y\" to y)\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "4037b651",
   "metadata": {},
   "outputs": [],
   "source": [
    "val data = getData(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "1709c86a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"nRhWjh\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"plot\">\n",
       "       (function() {\n",
       "           var plotSpec={\n",
       "\"layout\":{\n",
       "\"name\":\"grid\",\n",
       "\"ncol\":2,\n",
       "\"nrow\":2,\n",
       "\"fit\":true,\n",
       "\"align\":false\n",
       "},\n",
       "\"figures\":[{\n",
       "\"ggtitle\":{\n",
       "\"text\":\"Default\"\n",
       "},\n",
       "\"mapping\":{\n",
       "\"x\":\"x\",\n",
       "\"y\":\"y\"\n",
       "},\n",
       "\"data\":{\n",
       "\"x\":[-10.0,-9.0,-8.0,-7.0,-6.0,-5.0,-4.0,-3.0,-2.0,-1.0,0.0,1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0],\n",
       "\"y\":[-22026.465794806718,-8103.083927575384,-2980.9579870417283,-1096.6331584284585,-403.4287934927351,-148.4131591025766,-54.598150033144236,-20.085536923187668,-7.38905609893065,-2.718281828459045,0.0,2.718281828459045,7.38905609893065,20.085536923187668,54.598150033144236,148.4131591025766,403.4287934927351,1096.6331584284585,2980.9579870417283,8103.083927575384,22026.465794806718]\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[],\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"point\",\n",
       "\"data\":{\n",
       "}\n",
       "}]\n",
       "},{\n",
       "\"ggtitle\":{\n",
       "\"text\":\"trans='symlog'\"\n",
       "},\n",
       "\"mapping\":{\n",
       "\"x\":\"x\",\n",
       "\"y\":\"y\"\n",
       "},\n",
       "\"data\":{\n",
       "\"x\":[-10.0,-9.0,-8.0,-7.0,-6.0,-5.0,-4.0,-3.0,-2.0,-1.0,0.0,1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0],\n",
       "\"y\":[-22026.465794806718,-8103.083927575384,-2980.9579870417283,-1096.6331584284585,-403.4287934927351,-148.4131591025766,-54.598150033144236,-20.085536923187668,-7.38905609893065,-2.718281828459045,0.0,2.718281828459045,7.38905609893065,20.085536923187668,54.598150033144236,148.4131591025766,403.4287934927351,1096.6331584284585,2980.9579870417283,8103.083927575384,22026.465794806718]\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[{\n",
       "\"aesthetic\":\"y\",\n",
       "\"trans\":\"symlog\"\n",
       "}],\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"point\",\n",
       "\"data\":{\n",
       "}\n",
       "}]\n",
       "},{\n",
       "\"ggtitle\":{\n",
       "\"text\":\"trans='log10'\"\n",
       "},\n",
       "\"mapping\":{\n",
       "\"x\":\"x\",\n",
       "\"y\":\"y\"\n",
       "},\n",
       "\"data\":{\n",
       "\"x\":[-10.0,-9.0,-8.0,-7.0,-6.0,-5.0,-4.0,-3.0,-2.0,-1.0,0.0,1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0],\n",
       "\"y\":[-22026.465794806718,-8103.083927575384,-2980.9579870417283,-1096.6331584284585,-403.4287934927351,-148.4131591025766,-54.598150033144236,-20.085536923187668,-7.38905609893065,-2.718281828459045,0.0,2.718281828459045,7.38905609893065,20.085536923187668,54.598150033144236,148.4131591025766,403.4287934927351,1096.6331584284585,2980.9579870417283,8103.083927575384,22026.465794806718]\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[{\n",
       "\"aesthetic\":\"y\",\n",
       "\"trans\":\"log10\"\n",
       "}],\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"point\",\n",
       "\"data\":{\n",
       "}\n",
       "}]\n",
       "},{\n",
       "\"ggtitle\":{\n",
       "\"text\":\"trans='log2'\"\n",
       "},\n",
       "\"mapping\":{\n",
       "\"x\":\"x\",\n",
       "\"y\":\"y\"\n",
       "},\n",
       "\"data\":{\n",
       "\"x\":[-10.0,-9.0,-8.0,-7.0,-6.0,-5.0,-4.0,-3.0,-2.0,-1.0,0.0,1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0],\n",
       "\"y\":[-22026.465794806718,-8103.083927575384,-2980.9579870417283,-1096.6331584284585,-403.4287934927351,-148.4131591025766,-54.598150033144236,-20.085536923187668,-7.38905609893065,-2.718281828459045,0.0,2.718281828459045,7.38905609893065,20.085536923187668,54.598150033144236,148.4131591025766,403.4287934927351,1096.6331584284585,2980.9579870417283,8103.083927575384,22026.465794806718]\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[{\n",
       "\"aesthetic\":\"y\",\n",
       "\"trans\":\"log2\"\n",
       "}],\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"point\",\n",
       "\"data\":{\n",
       "}\n",
       "}]\n",
       "}],\n",
       "\"kind\":\"subplots\"\n",
       "};\n",
       "           var plotContainer = document.getElementById(\"nRhWjh\");\n",
       "           window.letsPlotCall(function() {{\n",
       "               LetsPlot.buildPlotFromProcessedSpecs(plotSpec, -1, -1, plotContainer);\n",
       "           }});\n",
       "       })();    \n",
       "   </script>"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "val p = letsPlot(data) { x = \"x\"; y = \"y\" } + geomPoint()\n",
    "\n",
    "gggrid(\n",
    "    listOf(\n",
    "        p + ggtitle(\"Default\"),\n",
    "        p + scaleYContinuous(trans=\"symlog\") + ggtitle(\"trans='symlog'\"),\n",
    "        p + scaleYContinuous(trans=\"log10\") + ggtitle(\"trans='log10'\"),\n",
    "        p + scaleYContinuous(trans=\"log2\") + ggtitle(\"trans='log2'\"),\n",
    "    ), \n",
    "    ncol=2\n",
    ")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Kotlin",
   "language": "kotlin",
   "name": "kotlin"
  },
  "language_info": {
   "codemirror_mode": "text/x-kotlin",
   "file_extension": ".kt",
   "mimetype": "text/x-kotlin",
   "name": "kotlin",
   "nbconvert_exporter": "",
   "pygments_lexer": "kotlin",
   "version": "1.8.20"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
